package com.bxl.chooser.util;



import com.bxl.netty.rpc.common.future.CompletableFuture;
import com.bxl.netty.rpc.common.message.MsgResponseContent;
import com.bxl.netty.rpc.common.message.PackageMsg;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 异步调用-保存消息和等待对象的关系
 *
 * @author baixl
 * @date 2021/2/25
 */
public class MessageRegister<T> {
    private static ConcurrentHashMap<Long, CompletableFuture> mapping = new ConcurrentHashMap<Long, CompletableFuture>();

    public static void  register(long requestID, CompletableFuture cb){
        mapping.putIfAbsent(requestID, cb);
    }
    public static void runCallBack(PackageMsg req){
        CompletableFuture cf = mapping.get(req.getHeader().getRequestID());
        cf.complete(req.getContent());
        removeCB(req.getHeader().getRequestID());
    }
    

    public static void removeCB(long requestID) {
        mapping.remove(requestID);
    }
}
